package com.qinyh.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qinyh.entity.Note;
import com.qinyh.util.CRDBHelper;
import com.qinyh.util.DateUtil;
import com.qinyh.util.TemplateStore;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class NoteActivity extends Activity {
	int type = 1;

	private Note currentNote;

	@Override
	protected void onCreate(Bundle b) {
		super.onCreate(b);
		Bundle bundle = this.getIntent().getExtras();
		type = Integer.valueOf(bundle.get("type").toString());
		String title = "";
		try {
			title = bundle.getString("title").toString();
		} catch (Exception exp) {

		}
		if (type == 1) {
			setContentView(R.layout.editnote);
			EditText notetitle = (EditText) findViewById(R.id.notetitle);
			notetitle.setText(title);
		} else if (type == 2) {
			{
				setContentView(R.layout.note);
				initNote();
			}
		}
	}
	List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	SimpleAdapter adapter=null;
	private void initNote() {
		CRDBHelper helper = new CRDBHelper(this);
		final List<Note> notes = helper.queryAllNote(TemplateStore
				.getCurrentBook().getId() + "");
		helper.close();
		ListView lv = (ListView) findViewById(R.id.note_list_title);
		TextView tv = (TextView) findViewById(R.id.addnoteimg);
		tv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				NoteActivity.this.setContentView(R.layout.editnote);
				NoteActivity.this.type = 1;
			}
		});
		
		for (Note note : notes) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("NAME", note.getNoteTitle());
			map.put("DATE", note.getNoteTime());
			list.add(map);
		}
		adapter = new SimpleAdapter(this, list,
				R.layout.note_adapter, new String[] { "NAME", "DATE" },
				new int[] { R.id.listitem_title, R.id.listitem_time });
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new ListView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				currentNote = notes.get(position);
				setContentView(R.layout.editnote);
				EditText notetitle = (EditText) findViewById(R.id.notetitle);
				EditText notecontent = (EditText) findViewById(R.id.notecontent);
				notetitle.setText(currentNote.getNoteTitle());
				notecontent.setText(currentNote.getNoteContent());
				type = 1;
			}
		});
		lv.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				currentNote = notes.get(position);
				delNote(position);
				Toast.makeText(NoteActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
				return true;
			}
		});

	}

	private void delNote(int position) {
		CRDBHelper db = new CRDBHelper(this);
		if (currentNote != null) {
			db.delNote(currentNote.getNoteTime());
		}
		db.close();
		list.remove(position);
		adapter.notifyDataSetChanged();
		currentNote=null;
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (type == 1) {
			CRDBHelper db = new CRDBHelper(this);
			if (currentNote != null) {
				db.delNote(currentNote.getNoteTime());
			}
			EditText notetitle = (EditText) findViewById(R.id.notetitle);
			if (notetitle.getText().toString() == null)
				return;
			if ("".equals(notetitle.getText().toString().trim()))
				return;
			EditText notecontent = (EditText) findViewById(R.id.notecontent);
			Note note = new Note();
			note.setBookId(TemplateStore.getCurrentBook().getId() + "");
			note.setNoteCatalog(null);
			note.setNoteTitle(notetitle.getEditableText().toString());
			note.setNoteContent(notecontent.getEditableText().toString());
			note.setOffset(TemplateStore.getCurrentStart() + "");
			if (currentNote != null) {
				if (currentNote.getNoteTime() == null
						|| "".equals(currentNote.getNoteTime()))
					note.setNoteTime(DateUtil.dateToString(new Date()));
				else
					note.setNoteTime(currentNote.getNoteTime());
			} else
				note.setNoteTime(DateUtil.dateToString(new Date()));
			currentNote = null;
			db.addNote(note);
			db.close();
			Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
		}
	}

}
